﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class customization_customization_info : System.Web.UI.Page, ICallbackEventHandler
{
    public string Message
    {
        get;
        set;
    }

    string _logo, _beijing, _figure;
    protected void Page_Load(object sender, EventArgs e)
    {
        string calllback = Page.ClientScript.GetCallbackEventReference(this, "args", "MakeMessage", "", "MakeError", false);
        string cabs = "function upLoad(args,context) \n" + "{ " + calllback + ";\n}";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "upLoad", cabs, true);
        if (Session["sJiGou"] == null || Session["sJiGou"].ToString() == string.Empty)
        {

        }
        mapJigouId();
        string sql = string.Format("select logo,logoQiYong,BeiJing,BeiJingQiYong,Figure,FigureEnabled from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJiGou"]);
        SqlDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, null);
        if (dr.Read())
        {
            if (Convert.ToBoolean(dr["logoQiYong"]))
            {
                if (dr["logo"].ToString().ToLower().IndexOf(".swf") > -1)
                {
                    chkLogo.Enabled = true;
                    //chkBg.Enabled = false;
                }
                else
                {
                    chkLogo.Enabled = false;
                    //chkBg.Enabled = true;
                }
            }
            else
            {
                chkLogo.Enabled = false;
            }
            chkFrame.Enabled = Convert.ToBoolean(dr["BeiJingQiYong"]);
            chkFigure.Enabled = Convert.ToBoolean(dr["FigureEnabled"]);
            _logo = dr["logo"].ToString();
            _beijing = dr["BeiJing"].ToString();
            _figure = dr["Figure"].ToString();
        }
        dr.Close();
    }

    public string GetCallbackResult()
    {
        return Message;
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        string sql = String.Empty;
        if (String.Compare(eventArgument, "upload") == 0)
        {
            string logo = string.Empty;
            string figure = String.Empty;
            bool figureToggle = false;
            bool logoToggle = false;
            string BeiJingTuPian = string.Empty;
            bool BeiJingQiYongToggle = false;
            if (File.Exists(MapPath(String.Format("../logos/{0}/logo_{0}.swf", Session["JigouId"]))))
            {
                logo = String.Format("logos/{0}/logo_{0}.swf", Session["JigouId"]);
                logoToggle = true;
            }
            if (File.Exists(MapPath(String.Format("../logos/{0}/figure_{0}.swf", Session["JigouId"]))))
            {
                figure = String.Format("logos/{0}/figure_{0}.swf", Session["JigouId"]);
                figureToggle = true;
            }
            if (File.Exists(MapPath(string.Format("../images/framebg/{0}/frame_{0}.gif", Session["JigouId"]))))
            {
                BeiJingTuPian = String.Format("images/framebg/{0}/frame_{0}.gif", Session["JigouId"]);
                BeiJingQiYongToggle = true;
            }
            sql = string.Format("update JiGouPeiZhi set logo='{0}',logoQiYong='{1}',BeiJing='{2}',BeiJingQiYong='{3}',Figure='{4}',FigureEnabled='{5}' where id={6}", logo, logoToggle, BeiJingTuPian, BeiJingQiYongToggle,figure,figureToggle, Session["JigouId"]);
        }
        else
        {
            bool logoQiYong = chkLogo.Enabled;
            bool BeiJingTuPianQiYong = chkFrame.Enabled;
            bool FigureEnabled = chkFigure.Enabled;

            string[] which = eventArgument.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            string delWhich = String.Empty;
            for (int i = 0; i < which.Length; i++)
            {
                delWhich += which[i] + ",";
            }
            if (delWhich.Contains("chkLogo"))
            {
                File.Delete(MapPath(String.Format("../{0}", _logo)));
                logoQiYong = false;
            }
            if (delWhich.Contains("chkFigure"))
            {
                File.Delete(MapPath(String.Format("../{0}", _figure)));
                FigureEnabled = false;
            }
            if (delWhich.Contains("chkFrame"))
            {
                File.Delete(MapPath(String.Format("../{0}", _beijing)));
                BeiJingTuPianQiYong = false;
            }
            sql = String.Format("update JiGouPeiZhi set logoQiYong='{0}',BeiJingQiYong='{1}',FigureEnabled='{2}' where id={3}", logoQiYong, BeiJingTuPianQiYong,FigureEnabled, Session["JigouId"]);
        }
        int result = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        if (result > 0)
        {
            Message = "success";
            if (String.Compare(eventArgument, "upload") != 0)
            {
                Message = "reLogin";
            }
        }
        else
        {
            Message = "failure";
        }
    }
    void mapJigouId()
    {
        string sql = string.Format("select count(id) from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"]);
        int num = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num == 0)
        {
            sql = string.Format("insert into JiGouPeiZhi (JiGouMingCheng) values('{0}')", Session["sJigou"]);
            num = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            if (num == 1)
            {
                sql = string.Format("select id from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"]);
                Session["JigouId"] = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>alert('{0}');</script>", "机构映射失败"));
            }
        }
        else
        {
            sql = string.Format("select id from JiGouPeiZhi where JiGouMingCheng='{0}'", Session["sJigou"]);
            Session["JigouId"] = DbHelper.ExecuteScalar(CommandType.Text, sql, null);
        }
    }
}
